package com.cqjtu.eecs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqjtu.eecs.entity.*;
import com.cqjtu.eecs.entity.dto.PracticeCourseDTO;
import com.cqjtu.eecs.entity.dto.TheoryCourseDTO;
import com.cqjtu.eecs.entity.vo.CourseTypeVo;
import com.cqjtu.eecs.entity.vo.TrainingProjectCourseVO;
import com.cqjtu.eecs.exception.NotFoundException;
import com.cqjtu.eecs.mapper.*;
import com.cqjtu.eecs.service.ITrainingProjectCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqjtu.eecs.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author
 * @since 2022-02-28
 */
@Service
public class TrainingProjectCourseServiceImpl extends ServiceImpl<TrainingProjectCourseMapper, TrainingProjectCourse> implements ITrainingProjectCourseService {

    @Autowired
    private TrainingProjectCourseMapper trainingProjectCourseMapper;


    /**
     * 通过专业代码获取模板中的课程
     * @param majorCode    专业代码
     * @return
     */
    @Override
    public TrainingProjectCourseVO getTrainingProjectCourseVOById(String majorCode) {
        // 通过专业代码获取到该专业的模板课程
        List<TrainingProjectCourse> trainingProjectCourses = trainingProjectCourseMapper.selectList(new LambdaQueryWrapper<TrainingProjectCourse>()
                .eq(TrainingProjectCourse::getMajorCode, majorCode));
        if (trainingProjectCourses.isEmpty())
            throw new NotFoundException("不存在对应课程模板或专业");

        try {
            TrainingProjectCourseVO trainingProjectCourseVO = new TrainingProjectCourseVO();

            // 通过专业代码获取专业理论课程
            List<CourseTypeVo> courseTypeList = trainingProjectCourseMapper.getTheoryCourseByMajorCode(majorCode);
            trainingProjectCourseVO.setTheoryCourseList(courseTypeList);
            return trainingProjectCourseVO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public TrainingProjectCourseVO getPracticalCourseListByMajorCode(String majorCode) {
        // 通过专业代码获取到该专业的模板课程
        List<TrainingProjectCourse> trainingProjectCourses = trainingProjectCourseMapper.selectList(new LambdaQueryWrapper<TrainingProjectCourse>()
                .eq(TrainingProjectCourse::getMajorCode, majorCode));
        if (trainingProjectCourses.isEmpty())
            throw new NotFoundException("不存在对应课程模板或专业");

        try {
            TrainingProjectCourseVO trainingProjectCourseVO = new TrainingProjectCourseVO();

            // 通过专业代码获取专业实践课程
            List<PracticeCourseDTO> practiceCourseList = trainingProjectCourseMapper.getPracticeCourseByMajorCode(majorCode);
            trainingProjectCourseVO.setPracticeCourseList(practiceCourseList);
            return trainingProjectCourseVO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
